function CategoryAdd_Code(id,parentid,name) {
	var buffer = new StringBuffer();
	buffer.append('<div id="parentid" val="'+id+'" parentid="'+parentid+'" class="crmsheet border-bottom"><div class="ctitle"><div class="cname">选择父类</div><div class="require">*</div></div><div class="ctext"><select id="txt_parentid"></select></div><div class="carrow"><img src="img/arrow.png"/></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">分类名称</div><div class="require">*</div></div><div class="ctext"><input id="txt_name" type="text" placeholder="填写分类名称" value="'+name+'"/></div><div class="carrow"></div></div>');
	if(id!=0){
	buffer.append('<div id="deletesheet" class="deletesheet" style="margin-top:80px;"><div class="name" style="width:100%; text-align:center;">删除该分类</div></div>');
	buffer.append('<div class="loading">若分类下含有子分类或产品，则不能删除该分类</div>');
	}
	return buffer.toString();
};


function Init_CategoryAdd(){
    var data = {};
	data.act = "Find_AllCategory";
	data.source=source;
	data.userid=userid;
	data.comid=comid;
	data.sign=GetSign(data.act);
	mui.getJSON(crm_domain, data, function (json) {
	    if (json.state) {
	    	var isselect="";
	    	var str="<option value='0'>无上级</option>";
	    	$.each(json.data.category,function(i,info){
	    		if(info.big.ID+""==$("#parentid").attr("parentid"))
	    		{
	    			isselect="selected='selected'";
	    		}
	    		else
	    		{
	    			isselect="";
	    		}
	    		str+='<option value="'+info.big.ID+'" '+isselect+'>'+info.big.Name+'</option>';
	    	});
	    	$("#txt_parentid").html(str);
	    }
	});
	
	if($("#parentid").attr("val")!="0")
		{
			get("deletesheet").addEventListener("tap",function(){
		        	plus.nativeUI.confirm("确定要删除吗？", function(event){
						if(event.index==1)
						{
							var data = {};
						    data.act = "Delete_Category";
						    data.id = $("#parentid").attr("val");
						    data.userid=userid;
							data.comid=comid;
							data.source=source;
							data.sign=GetSign(data.act);
						    mui.getJSON(crm_domain, data, function(json) {
								if(json.state)
								{
									obj.hide(300);
									CloseWindow();
									plus.nativeUI.toast(json.msg);
								}else
								{
									plus.nativeUI.alert(json.msg);
								}
								
						    });	
						}
					},"提示", ["取消","确认"]); 
				
			});
		}

};




function Save_Category() {
        var data = {};
        var txt_name = $("#txt_name");
        data.id = $("#parentid").attr("val");
        if(txt_name.val().Trim()=="")
        {
        	plus.nativeUI.toast("请输入分类名称");
        	return false;
        }
		if(data.id=="0")
		{
        	data.act = "Add_Category";
        }else
        {
       		data.act = "Edit_Category";
        }
        data.parentid= $("#txt_parentid").val();
        data.userid=userid;
		data.comid=comid;
        data.name = $("#txt_name").val();
        data.source = source; 
        data.version = version;
        data.sign=GetSign(data.act);
        mui.getJSON(crm_domain, data, function (json) {
        	plus.nativeUI.toast(json.msg);
            if (json.state) {
                CloseWindow();
                Receive_Category(1);
                
            } else {
				plus.nativeUI.toast(json.msg); 
            }
        });
};
